package com.hans.scala.exception

/**
 * User: ZhangJuntao
 * Date: 2014/3/26
 * Time: 13:51
 */
object ExceptionHandler {
  def main(args: Array[String]) {
    def testException(n: Int) {
      try {
        if (n == 0) throw new ZeroException("n is 0")
        val half =
          if (n % 2 == 0) n / 2
          else throw new EvenException("n must be even")
        println(half)
      } catch {
        case ex: ZeroException =>
          println("handling ZeroException")
          println("handled ZeroException")
        case ex: EvenException =>
          println("handling EvenException")
          println("handled EvenException")
      } finally {
        println("end\n")
      }
    }

    testException(4)
    testException(3)
    testException(0)
  }


}
